/**
 * 
 */
package com.luckmgr.web;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Company;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.UserService;


/**
 * 
 * @author duanhx
 * @version [Ver 1.0; 2012-7-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	UserService userService;

	@RequestMapping("")
	public String userindex(Model model,HttpSession session,String uname,@RequestParam(required = false)  Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", uname);
		PageBean<User> pagebean = userService.selectListPage(map, page);
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("uname", uname);
		//查询公司列表 密是admin的管理员
		return "user/index";
	}
	

	
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer id) {
		if(null != id){
			User user = userService.selectByPrimaryKey(id);
			model.addAttribute("entity", user);
			return "user/addup";
		}
		return "user/addup";
	}
	
	
	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			userService.deleteByPrimaryKey(id);
		}
		return "redirect:/user";
	}
	
	
	
	@RequestMapping(value = "update")
	public String userupdate(Model model,@ModelAttribute("entity") User user) {
		if(null == user){
			model.addAttribute("msg", "数据为空！");
			return "user/addup";
		}
		if( StringUtils.isNull(user.getUserPwd()) || 0 == user.getUserLevel()){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "user/addup";
		}
		
		if(user.getId()!=null && 0!=user.getId()){
			user.setUserName(null);
			userService.updateEntity(user);
		}else{
			if(StringUtils.isNull(user.getUserName()) ){
				model.addAttribute("msg", "数据不完整请重新录入！");
				return "user/addup";
			}
			userService.addEntity(user);
		}
		return "redirect:/user";
	}
	
	
}
